package com.xqb.listen.ui

import android.Manifest
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.xqb.listen.R
import com.xqb.listen.manager.MediaPlayManager
import com.xqb.listen.manager.permission.PermissionUtil
import com.xqb.listen.navigation.INavigation
import com.xqb.listen.ui.theme.ListenAppComposeTheme

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.Theme_ListenAppCompose)
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ListenAppComposeTheme {
                Box(Modifier.fillMaxSize()){
                    INavigation()
                }
            }
        }
        startMService()
    }


    private fun startMService(){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !PermissionUtil.hasNotifyPermission(this)){
            registerForActivityResult(ActivityResultContracts.RequestPermission()){
                if(it){
                    MediaPlayManager.singleInstance.initAudio(this@MainActivity , true,null)
                }
            }.launch(Manifest.permission.POST_NOTIFICATIONS)
        }else{
            MediaPlayManager.singleInstance.initAudio(this@MainActivity , true,null)
        }
    }
}

@Preview(showBackground = true , showSystemUi = true)
@Composable
fun PreviewRoot(){
    ListenAppComposeTheme {
        INavigation()
    }
}